import router from './router'
import Vue from 'vue'
import store from './store'
import {Message} from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'


NProgress.configure({showSpinner: false}) // NProgress Configuration

const whiteList = ['/login', '/404'] // no redirect whitelist

router.beforeEach(async (to, from, next) => {
    // start progress bar
    store.commit("user/SET_NAME",Vue.ls.get("name"));
    NProgress.start()
    // set page title
    document.title = getPageTitle(to.meta.title);
    // console.log(to)
    if (Vue.ls.get("name")) {
        // 用户是否登录
        if (to.path === '/login') {
            // 登录了直接跳转到首页
            next({path: '/'})
            NProgress.done();
            return;
        }
        NProgress.done();
        next();
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            // 白名单
            next()
            NProgress.done();
        } else {
            // other pages that do not have permission to access are redirected to the login page.
            next(`/login?redirect=${to.path}`)
            NProgress.done();
        }
    }
})

router.afterEach(() => {
    // finish progress bar
    NProgress.done()
})
